<?php

class ThemeController extends BackOfficeController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	public function actionAdmin()
    {
        $themes = array();
        $selectedTheme = array();
        $themesDir = Yii::getPathOfAlias('site').'/../themes/';
        if ($dh = opendir($themesDir))
        {
            while (($file = readdir($dh)) !== false)
            {
                $path = $themesDir.$file;
                if (is_dir($path) && !in_array($file,array('.','..','.svn')))
                {
                    $theme = array('name'=>$file);
                    // check screenshot in theme folder
                    $arrExts = array('jpg','jpeg','png','gif','bmp','JPG','JPEG','PNG','GIF','BMP');
                    foreach($arrExts as $ext)
                    {
                        $screenshot_filepath = $path.'/screenshot.'.$ext;
                        if (file_exists($screenshot_filepath))
                        {
                            $theme['screenshot'] = str_replace('\\','/',baseUrl().'/sites/'.SITE_DIR.'/themes/'.$file.'/screenshot.'.$ext);
                            break;
                        }
                    }
                    if (!isset($theme['screenshot']))
                        $theme['screenshot'] = baseUrl().themeUrl().'/images/no_image.gif';    
                    
                    if ($theme['name'] == Settings::THEME)
                        $selectedTheme = $theme;
                    else
                        $themes[] = $theme;
                }            
            }
            closedir($dh);
        }

        //var_dump($themes);
        $this->render('admin',array('currentTheme'=>$selectedTheme,'otherAvailableThemes'=>$themes));
    }
    
    public function actionPreview()
    {
        $themeName = $this->get('id','');
        $themePath = Yii::getPathOfAlias('site').'/../themes/'.$themeName;
        if (!is_dir($themePath))
            throw new CHttpException(404,'Data is invalid');
        $theme = array('name'=>$themeName);
        $arrExts = array('jpg','jpeg','png','gif','bmp','JPG','JPEG','PNG','GIF','BMP');
        foreach($arrExts as $ext)
        {
            $screenshot_filepath = $themePath.'/screenshot.'.$ext;
            if (file_exists($screenshot_filepath))
            {
                $theme['screenshot'] = str_replace('\\','/',baseUrl().'/sites/'.SITE_DIR.'/themes/'.$themeName.'/screenshot.'.$ext);
                break;
            }
        }
        if (!isset($theme['screenshot']))
            $theme['screenshot'] = baseUrl().themeUrl().'/images/no_image.gif';
        
        $this->render('preview',array('theme'=>$theme));
    }
    
    public function actionActivate()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $themeName = $this->get('id','');
            $themePath = Yii::getPathOfAlias('site').'/../themes/'.$themeName;
            if (!is_dir($themePath))
            {
                echo "Theme is not found";
                Yii::app()->end();
            }
            
            $theme = SettingParam::model()->find('name=:name',array(':name'=>'THEME'));
            if (is_null($theme))
            {
                echo "Theme Parameter is not found";
                Yii::app()->end();
            }
            $theme->value = $themeName;
            $theme->update(array('value'));
            FSM::run('Core.Settings.db2php', array('module' => ''));
        }
    }
    
    public function actionApply()
    {
        if (Yii::app()->request->isPostRequest && isset($_POST['selected_theme']))
        {
            $this->render('apply',array('selected_theme'=>$_POST['selected_theme']));
        }
    }
}
